cmake_minimum_required(VERSION 3.16)

set(CMAKE_C_COMPILER_FORCED ON)
set(CMAKE_CXX_COMPILER_FORCED ON)

set(ARCHFLAGS -mthumb -mcpu=cortex-m0plus)
set(LDFLAGS --specs=nano.specs -Wl,--gc-sections,-Map,main.map,-T${CMAKE_SOURCE_DIR}/link.ld)

project(kl25z)

add_custom_target(${PROJECT_NAME}.elf
  COMMAND ${CMAKE_C_COMPILER} -c ${ARCHFLAGS} -o ${CMAKE_BINARY_DIR}/startup.o ${CMAKE_SOURCE_DIR}/startup.c
  COMMAND ${CMAKE_CXX_COMPILER} ${ARCHFLAGS} ${LDFLAGS} -o ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.elf
  ${CMAKE_SOURCE_DIR}/main.cpp
  ${CMAKE_SOURCE_DIR}/device/TSISensor.cpp
  ${CMAKE_SOURCE_DIR}/device/RGBLed.cpp
  ${CMAKE_SOURCE_DIR}/device/clock.cpp
  ${CMAKE_BINARY_DIR}/startup.o)

  # COMMAND sshpass -p "1" scp ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.srec yang@10.20.13.112:/media/yang/FRDM-KL25Z
add_custom_target(${PROJECT_NAME}.srec
  COMMAND ${CMAKE_OBJCOPY} -O srec ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.elf ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.srec
  COMMAND cp ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.srec /media/uos/FRDM-KL25Z
  DEPENDS ${PROJECT_NAME}.elf)